﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.Ptoduction;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.SCWorkCommand;
using SmartProduction.Read.API.Dto;

namespace SmartProduction.Read.API.Application.CommandHandlers.SCWorkCommandHandler
{
    public class SCWorkOrderXQQueryCommandHandler : IRequestHandler<SCWorkOrderXQQueryCommand, APIPaing<List<SCWorkOrderXQDtos>>>
    {

        private readonly IMapper mapper;
        /// <summary>
        /// 基本信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo;
        /// <summary>
        /// 物品信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionItem> wRepo;
        /// <summary>
        /// 工序流程仓储接口
        /// </summary>
        private readonly IBaseRepository<ProcessFlowSheet> lRepo;
        /// <summary>
        /// 物品名称仓储接口
        /// </summary>
        private readonly IBaseRepository<item_classification_detail> wnRepo;
        /// <summary>
        /// 单号仓储接口
        /// </summary>
        private readonly IBaseRepository<AssociatedProductionOrder> dhRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="jRepo"></param>
        public SCWorkOrderXQQueryCommandHandler(IMapper mapper, IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo, IBaseRepository<ProductionItem> wRepo, IBaseRepository<ProcessFlowSheet> lRepo, IBaseRepository<item_classification_detail> wnRepo, IBaseRepository<AssociatedProductionOrder> dhRepo)
        {
            this.mapper = mapper;
            this.jRepo = jRepo;
            this.wRepo = wRepo;
            this.lRepo = lRepo;
            this.wnRepo = wnRepo;
            this.dhRepo = dhRepo;
        }



        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaing<List<SCWorkOrderXQDtos>>> Handle(SCWorkOrderXQQueryCommand request, CancellationToken cancellationToken)
        {

            APIPaing<List<SCWorkOrderXQDtos>> result = new APIPaing<List<SCWorkOrderXQDtos>>();
            var list = from a in jRepo.GetAll().Result
                       join b in wRepo.GetAll().Result on a.ProductionItemId equals b.ProductionItemId
                       join c in wnRepo.GetAll().Result on b.itemName equals c.item_classification_detailID
                       join d in lRepo.GetAll().Result on a.ProcessFlowSheetId equals d.ProcessFlowSheetId
                       join e in dhRepo.GetAll().Result on a.AssociatedProductionOrderNumber equals e.AssociatedProductionOrderId
                       select new SCWorkOrderXQDtos
                       {
                           WorkOrderId = a.WorkOrderId,
                           WorkOrderNumber=a.WorkOrderNumber,
                           WorkOrderName = a.WorkOrderName,
                           AssociatedProductionOrderNumber = a.AssociatedProductionOrderNumber,
                           SalesOrder = a.SalesOrder,
                           AssociatedProject = a.AssociatedProject,
                           WorkOrderDate = a.WorkOrderDate,
                           PersoninCharge = a.PersoninCharge,
                           AffiliatedDepartment = a.AffiliatedDepartment,
                           Remarks = a.Remarks,
                           WenJian = a.WenJian,
                           itemName = b.itemName,
                           itemNumber = b.itemNumber,
                           specification = b.specification,
                           unit = b.unit,
                           itemCategory = b.itemCategory,
                           itemClassification = b.itemClassification,
                           plannedStartTime = b.plannedStartTime,
                           plannedEndTime = b.plannedEndTime,
                           plannedOutput = b.plannedOutput,
                           ProcessFlowSheetId = d.ProcessFlowSheetId,
                           ProcessCode = d.ProcessCode,
                           ProcessName = d.ProcessName,
                           ProcessingOrder = d.ProcessingOrder,
                           ProcessFlow = d.ProcessFlow,
                           PesponsiblePerson = d.PesponsiblePerson,
                           Pepartment = d.Pepartment,
                           PlannedStartDate = d.PlannedStartDate,
                           PlannedEndDate = d.PlannedEndDate,
                           PlannedQuantity = d.PlannedQuantity,
                           SCDHao = e.AssociatedProductionName,
                           SCWName = c.item_name

                       };
            if (request.WorkOrderId != null)
            {
                list = list.Where(x => x.WorkOrderId == request.WorkOrderId);
            }
            result.TotalCount = list.Count();
            result.PageCount = (int)Math.Ceiling((double)result.TotalCount / request.PageSize);
            list = list.OrderByDescending(o => o.WorkOrderId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            var data = mapper.Map<List<SCWorkOrderXQDtos>>(list);
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
            result.data = data;
            return await Task.FromResult(result);
        }
    }
}
